/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

 $(function(){
	$(":text,:password,:file,select,textarea").mouseover(function(){$(this).css('border','1px solid #00CCFF');}).mouseout(function(){$(this).css('border','1px solid #6688AA');}).focus(function(){$(this).css('border','1px solid #FF8800');});
	$(".top_menu li").mouseover(function(){$(this).addClass("mover")}).mouseout(function(){$(this).removeClass("mover");});
	$(".left_menu ul li").click(function(){$(".left_menu ul li").removeClass("active");$(this).addClass("active")});
	$("#left_menu ul li").mouseover(function(){$(this).addClass("hover")}).mouseout(function(){$(this).removeClass("hover");});
});


var dialog = {
	iframeNo: 0,
	agent: function (v){return (Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); },
	xy: function (e,v){return (v?(this.agent('msie')?event.clientY+document.body.scrollTop:e.pageY):(this.agent('msie')?event.clientX+document.body.scrollTop:e.pageX)); },
	drag: function (d,e)
	{
		var oX=parseInt(d.style.left),oY=parseInt(d.style.top),eX=this.xy(e),eY=this.xy(e,1),tX,tY,stop;
		document.onmousemove=function(e){if(!stop){ d.style.top=(tX=dialog.xy(e,1)+oY-eY+'px'); d.style.left=(tY=dialog.xy(e)+oX-eX+'px'); }};
		document.onmouseup=function(){stop=1; document.onmousemove=''; document.onmouseup='';};
	},
	open: function (id, name, str, button, align, size)
	{
		switch( size )
		{
			case 1:
				w = 800;h = 450;break;
			case 2:
				w = 600;h = 350;break;
			case 3:
				w = 400;h = 230;break;
			case 4:
				w = 300;h = 150;break;
		}
		var x = (document.body.scrollWidth-w)*0.5;
		var y = (document.body.scrollHeight-h)*0.5;
		var zIndex=0;
		if(this.iframeNo==0&&id!=0){this.dialogBg('open');zIndex=950;this.iframeNo=1;}else if(this.iframeNo==1&&id!=0){this.iframeNo=2;zIndex=951}else{zIndex=999}
		var dialog_id = id==0?'dialog_alert':'dialog_'+id;
		var close = id==0?'alertClose();':'dialogClose('+id+');';
		var html = '<div style="position: absolute; display: block; z-index: '+zIndex+'; left: '+x+'px; top: '+y+'px;width:'+w+'px;" onmousedown="dialog.drag(this,event)" id="'+dialog_id+'"><table cellspacing="0" cellpadding="0" border="0"><tbody><tr style="cursor: move;"><td height="33" width="13" style="background-image: url(images/system/dialog/dialog_lt.png) ! important;"><div style="width: 13px;"></div></td><td height="33" style="background-image: url(images/system/dialog/dialog_ct.png) ! important;"><div style="padding: 9px 0pt 0pt 4px; float: left; font-weight: bold; color: rgb(255, 255, 255);"><img align="absmiddle" src="images/system/dialog/icon_dialog.gif"/> '+name+' </div><div onclick="dialog.'+close+'"  onmouseout="this.style.backgroundImage=\'url(images/system/dialog/dialog_closebtn.gif)\'" onmouseover="this.style.backgroundImage=\'url(images/system/dialog/dialog_closebtn_over.gif)\'" style="margin: 5px 0pt 0pt; position: relative; cursor: pointer; float: right; height: 17px; width: 28px; background-image: url(images/system/dialog/dialog_closebtn.gif);"></div></td><td height="33" width="13" style="background-image: url(images/system/dialog/dialog_rt.png) ! important;"><div style="width: 13px;"></div></td></tr><tr><td width="13" style="background-image: url(images/system/dialog/dialog_mlm.png) ! important;"></td><td align="center" valign="top" style="background:#ffffff;"><div style="overflow: visible;height:'+h+'px;width:'+w+'px" scrolling="auto" ><table height="100%" id="dialog_content" cellspacing="0" cellpadding="10" border="0"><tbody><tr align="left" valign="top"><td>'+str+'</td></tr><tr height="36px"><td><div style="border-top: 1px solid rgb(218, 222, 229); padding: 8px 20px; text-align: '+align+'; background-color: rgb(246, 246, 246);">'+button+'</div></td></tr></tbody></table></div></td><td width="13" style="background-image: url(images/system/dialog/dialog_mrm.png) ! important;"/></tr><tr><td height="13" width="13" style="background-image: url(images/system/dialog/dialog_lb.png) ! important;"/><td style="background-image: url(images/system/dialog/dialog_cb.png) ! important;"/><td height="13" width="13" style="background-image: url(images/system/dialog/dialog_rb.png) ! important;"/></tr></tbody></table></div>';
		$("#dialog").append(html);
	},
	dialogClose: function (id){$("#dialog #dialog_"+id).remove();if(this.iframeNo==1){this.dialogBg('close');this.iframeNo=0;}else{this.iframeNo=1;}},
	alertClose: function (){$("#dialog #dialog_alert").remove();this.alertBg('close');},
	dialogBg: function (action){action == 'open'?$("#dialog #_dialogbg").fadeIn():$("#dialog #_dialogbg").fadeOut();},
	alertBg: function (action){action == 'open'?$("#dialog #_alertbg").show():$("#dialog #_alertbg").hide();},
	alert: function (msg,closeDialogId)
	{
		var onclick = (typeof closeDialogId == "undefined")?'dialog.alertClose();':'dialog.alertClose();dialog.dialogClose('+closeDialogId+');';
		var html = '<div style="text-align:center;float:left;width:100%;"><img height="34" align="left" width="34" style="padding:5px;" src="images/system/icon_alert.gif" /><div style="padding-top:40px;width:100%;z-index:1;position:absolute;top:40;left:0;" id="msg">'+msg+'</div></div>';
		var button = '<a class="slbutton" href="javascript:void(1);" onclick="'+onclick+'"><input type="button" class="srbutton" value="确 定"/></a>';
		this.alertBg('open');
		this.open(0,'系统提示',html,button,'center',4);
	},
	query: function (msg,func)
	{
		var html = '<div style="text-align:center;float:left;width:100%;"><img height="34" align="left" width="34" style="padding:5px;" src="images/system/icon_query.gif" /><div style="padding-top:40px;width:100%;z-index:1;position:absolute;top:40;left:0;" id="msg">'+msg+'</div></div>';
		var button = '<a class="slbutton" href="javascript:void(1);" onclick="'+func+';dialog.alertClose();"><input type="button" class="srbutton" value="确 定"/></a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="slbutton" href="javascript:void(1);"><input type="button" class="srbutton" onclick="dialog.alertClose();" value="取 消"/></a>';
		this.alertBg('open');
		this.open(0,'信息确认',html,button,'center',4);
	},
	iframe: function(id,name,url,yesButton,noButton,size)
	{
		var buttonLeft = '<a class="slbutton" href="javascript:void(1);">';
		var buttonRight = '</a>';
		var yesButtonHtml = '';
		if(yesButton==1){yesButtonHtml='<input type="button" class="srbutton" onclick="window.frames['+id+'].document.getElementById(\'sbt\').click();" value="确 定"/>';}
		else if(yesButton==2){yesButtonHtml='<input type="button" class="srbutton" onclick="dialog.dialogClose('+id+');" value="确 定"/>';}
		var noButtonHtml = '<input type="button" class="srbutton" onclick="dialog.dialogClose(1);" value="取 消"/>'
		var button = (yesButtonHtml!=''?buttonLeft+yesButtonHtml+buttonRight:'')+((yesButton!=2&&noButton)?'&nbsp;&nbsp;&nbsp;&nbsp;'+buttonLeft+noButtonHtml+buttonRight:'');
		var html = '<iframe id="dialog_iframe" scrolling="auto" width="100%" frameborder="0" height="100%" src="'+SITE_URL+SCRIPT+url+'"></iframe>';
		this.open(id,name,html,button,'right',size);
	}
}

var logout = function(){window.location=SITE_URL+SCRIPT+'?m=login&a=logout';}
var menu = function(o){$(".top_menu li").removeClass("over").eq(o).addClass("over");$("#left_menu ul").addClass("close").eq(o).removeClass("close");}
var gomain = function(str){$("#main").attr('src',SITE_URL+SCRIPT+'?m='+str+'&tmp='+Math.random());}

var iframeTest = function()
{
	var url = '?m=home&a=home';
	dialog.iframe(1,'dialog-iframe 测试！',url,1,1,1);
}

var getMsg = {
	
}


var salert = function(msg){parent.dialog.alert(msg);}
var sopen = function(str){$("#s_"+str).show();}
var sclose = function(str){$("#s_"+str).hide();}
var sinsert = function(str){$("#"+str[0]).val(str[1]);sclose(str[0])}

//全选/反选
var checkall = function ()
{
	if($("#checkbox").attr("checked")==true){$(".checkbox").each(function(){ $(this).attr("checked", 'true'); $(this).parent().parent().addClass('tr_selected');});}
	else{$(".checkbox").each(function(){ $(this).removeAttr("checked");$(this).parent().parent().removeClass("tr_selected");});}
}

//获取选中的checkbox
var getChecked = function()
{
	var checked = new Array();
	$(".checkbox").each(function(){
		if($(this).attr('checked')==true){
			checked.push($(this).val());
		}
	});
	return checked;
}

/**editor-dialog宽高调节**/
var sceditor = function(w,h){$(".ke-dialog").css({"width":(w+20)+'px',"height":(h+63)+'px'});$(".sciframe").css({"width":w+'px',"height":h+'px'});}

var colorSelect = function(){parent.dialog.iframe(1,'颜色选择器','?m=dialog&a=color',0,1,3);}

var tabId=1;
var stab = function (n)
{
	if(this.tabId!=n)
	{
		$(".nav_info div").removeClass('info').addClass('info2');$(".nav_info div").eq(n-1).removeClass('info2').addClass('info');
		$("#tab_container div#stab_"+this.tabId).fadeOut("fast", function(){$("#stab_"+n).fadeIn();});this.tabId = n;
	}
}

var app = {
	color: function(str){parent.window.frames[0].$("#color").val(str);parent.dialog.dialogClose(1);},
	special: function()
	{
		var special = '';
		$(".checkbox").each(function(){
			if( $(this).attr('checked') == true )
			{
				var sid = $(this).val();
				special += sid+' ';
			}
		});
		parent.window.frames[0].$("#special").val(special);
		parent.dialog.dialogClose(1);
	},
	articleEdit: function(aid,page){parent.$("#main").attr('src',SITE_URL+SCRIPT+'?m=article&a=edit&do=editarticle&id='+aid+'&page='+page);parent.dialog.dialogClose(1);},
	delPageConfirm: function(aid,page){parent.dialog.query('此操作不可恢复，是否确定删除？','app.delPage('+aid+','+page+')');},
	delPage: function(aid,page)
	{
		$("#dialog_iframe").attr('src',SITE_URL+SCRIPT+'?m=article&a=mag&do=delpage&id='+aid+'&page='+page);
		parent.window.frames[0].$("#tr_"+aid+' #pageno').text(parent.window.frames[0].$("#tr_"+aid+' #pageno').text()-1);
	},
	categoryArticleList: function(cid){parent.$("#main").attr('src',SITE_URL+SCRIPT+'?m=article&a=mag&do=list&type=cat&id='+cid);parent.dialog.dialogClose(1);},
	newArticle: function(cid){parent.$("#main").attr('src',SITE_URL+SCRIPT+'?m=article&a=new&do=newarticle&cat='+cid);parent.dialog.dialogClose(1);},
	articleCopy: function()
	{
		var ids = $("#ids").val();
		var cids = getChecked();
		if(cids=='')
		{
			salert('请选择要复制到的文档分类！');
			return fasle;
		}
		var strCids = cids.join('_');
		parent.dialog.alert('正在复制中，请稍后。。。',1);
		$.ajax({
			type: "POST",
			url: SITE_URL+SCRIPT+"?m=article&a=mag&do=copy",
			data: {'ids':ids,'cids':strCids},
			dataType:'json',
			error: function (xmlHttpRequest, error) {
				parent.$("#dialog_alert #msg").text('AJAX数据获取异常！');
			},
			success: function(rs)
			{
				parent.$("#dialog_alert #msg").text(rs?'成功复制文档！':'复制文档失败！');
				parent.main.location.reload();
			}
		});
	},
	articleMove: function()
	{
		var ids = $("#ids").val();
		var cid = getChecked();
		if(cid=='')
		{
			salert('请选择要转移到的文档分类！');
			return fasle;
		}
		parent.dialog.alert('正在转移中，请稍后。。。',1);
		$.ajax({
			type: "POST",
			url: SITE_URL+SCRIPT+"?m=article&a=mag&do=move",
			data: {'ids':ids,'cid':cid},
			dataType:'json',
			error: function (xmlHttpRequest, error) {
				parent.$("#dialog_alert #msg").text('AJAX数据获取异常！');
			},
			success: function(rs)
			{
				parent.$("#dialog_alert #msg").text(rs?'成功转移文档！':'转移文档失败！');
				parent.main.location.reload();
			}
		});
	},
	articleDel: function(ids)
	{
		salert('正在删除中，请稍后。。。');
		$.ajax({
			type: "POST",
			url: SITE_URL+SCRIPT+"?m=article&a=mag&do=delarticle",
			data: {'ids':ids},
			dataType:'json',
			error: function (xmlHttpRequest, error) {
				parent.$("#dialog_alert #msg").text('AJAX数据获取异常！');
			},
			success: function(rs)
			{
				parent.$("#dialog_alert #msg").text(rs?'成功删除文档！':'删除文档失败！');
				parent.main.location.reload();
			}
		});
	}
}